package com.aiyige.page.publish.guarantee.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.page.publish.guarantee.model.GuaranteeItem;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import net.cachapa.expandablelayout.ExpandableLayout;

import java.util.LinkedList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zhuyakun on 2017/11/7.
 */

public class GuaranteeAdapter extends BaseQuickAdapter<GuaranteeItem, GuaranteeAdapter.ViewHolder> {


    public GuaranteeAdapter() {
        super(R.layout.guarantee_item, new LinkedList<GuaranteeItem>());
    }

    @Override
    protected void convert(ViewHolder helper, GuaranteeItem item) {
        helper.bindData(item);
    }


    public class ViewHolder extends BaseViewHolder {
        @BindView(R.id.icon)
        ImageView icon;
        @BindView(R.id.titleTv)
        TextView titleTv;
        @BindView(R.id.expandBtn)
        ImageView expandBtn;
        @BindView(R.id.contentTv)
        TextView contentTv;
        @BindView(R.id.expandable_layout)
        ExpandableLayout expandableLayout;
        GuaranteeItem data;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void bindData(GuaranteeItem item) {
            data = item;
            if (data.isSelected()) {
                icon.setVisibility(View.VISIBLE);
            } else {
                icon.setVisibility(View.INVISIBLE);
            }
            titleTv.setText(data.getTitle());
            contentTv.setText(data.getContent());
            if (data.isExpanded()) {
                expandBtn.setRotation(180);
                expandableLayout.expand(false);
            } else {
                expandBtn.setRotation(0);
                expandableLayout.collapse(false);
            }
        }

        @OnClick({R.id.expandBtn, R.id.containerLayout})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.expandBtn:
                    data.setExpanded(!data.isExpanded());
                    if (data.isExpanded()) {
                        expandBtn.animate().rotation(180).start();
                        expandableLayout.expand(true);
                    } else {
                        expandBtn.animate().rotation(0).start();
                        expandableLayout.collapse(true);
                    }
                    break;
                case R.id.containerLayout:
                    data.setSelected(!data.isSelected());
                    if (data.isSelected()) {
                        icon.setVisibility(View.VISIBLE);
                    } else {
                        icon.setVisibility(View.INVISIBLE);
                    }
                    break;
            }
        }
    }
}
